home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Shareware World
/
Utilities
/
Text Processing
/
Alpha
/
Tcl
/
SystemCode
/
diff.tcl
< prev
next >
Wrap
Text File
|
1996-08-15
|
8KB
|
299 lines
# Usage: diff [-#] [-abBcdefhHilnNprstTuvw] [-C lines] [-F regexp] [-I regexp]
# [-L label [-L label]] [-S file] [-D symbol] [+ignore-blank-lines]
# [+context[=lines]] [+unified[=lines]] [+ifdef=symbol]
# [+show-function-line=regexp]
# [+speed-large-files] [+ignore-matching-lines=regexp] [+new-file]
# [+initial-tab] [+starting-file=file] [+text] [+all-text] [+ascii]
# [+minimal] [+ignore-space-change] [+ed] [+reversed-ed] [+ignore-case]
# [+print] [+rcs] [+show-c-function] [+binary] [+brief] [+recursive]
# [+report-identical-files] [+expand-tabs] [+ignore-all-space]
# [+file-label=label [+file-label=label]] [+version] path1 path2
proc compareDirectories {} {
global diffOne diffTwo
# return "Closed for repairs…"
set diffOne [string trimright [get_directory -p "Select dir 1:"] {:}]
set diffTwo [string trimright [get_directory -p "Select dir 2:"] {:}]
doTheCompare 1 {* Directory Comparison *}
}
proc compareFiles {} {
global tileWidth tileHeight tileTop tileLeft
global diffOne diffTwo
set diffOne [getfile "Select your first file:"]
set diffTwo [getfile "Select your second file:"]
if {[lsearch [winNames -f] $diffOne] >= 0} {
bringToFront $diffOne
killWindow
if {[lsearch [winNames -f] $diffOne] >= 0} {return}
}
if {[lsearch [winNames -f] $diffTwo] >= 0} {
bringToFront $diffTwo
killWindow
if {[lsearch [winNames -f] $diffTwo] >= 0} {return}
}
set geo1 [diffGeo 1]
set geo2 [diffGeo 0]
edit -g [lindex $geo1 0] [lindex $geo1 1] [lindex $geo1 2] [lindex $geo1 3] $diffOne
set diffOne [car [winNames -f]]
display 0
goto 0
edit -g [lindex $geo2 0] [lindex $geo2 1] [lindex $geo2 2] [lindex $geo2 3] $diffTwo
set diffTwo [car [winNames -f]]
display 0
goto 0
doTheCompare
}
proc diffGeo {left} {
global tileWidth tileHeight tileTop tileLeft
set margin 4
set width [expr ($tileWidth - $margin)/2]
set height [expr $tileHeight - 200]
set hor $tileLeft
if {!$left} {incr hor [expr $width+$margin]}
return [list $hor $tileTop $width $height]
}
proc compareWindows {} {
global tileHeight tileWidth tileTop tileLeft
global diffOne diffTwo
set wins [winNames -f]
if {[llength $wins] < 2} { message "Need 2 windows"; return }
set diffOne [lindex $wins 0]
set diffTwo [lindex $wins 1]
for {set i 0} {$i < 2} {incr i} {
set nm [lindex $wins $i]
getWinInfo -w $nm b
if {$b(dirty) == 1} {
alertnote "Can't compare because the window $nm has unsaved changes. \
Save it and try again."
return
}
}
set geo0 [diffGeo 1]
set geo1 [diffGeo 0]
for {set i 0} {$i < 2} {incr i} {
moveWin [lindex $wins $i] 1000 0
sizeWin [lindex $wins $i] [lindex [set geo$i] 2] [lindex [set geo$i] 3]
}
for {set i 0} {$i < 2} {incr i} {
moveWin [lindex $wins $i] [lindex [set geo$i] 0] [lindex [set geo$i] 1]
}
doTheCompare
}
proc trimnum {str} {
if {[regexp -indices { <[0-9]+>$} $str ind]} {
return [string range $str 0 [expr [lindex $ind 0] - 1]]
} else {
return $str
}
}
# proc trimB {wname} {regsub { <[0-9]*>$} $wname "" wname;return $wname}
proc doTheCompare {{isdir 0} {name {* File Comparison *}}} {
global tileLeft tileTop tileWidth tileHeight diffFlags
global diffOne diffTwo winModes HOME diffDir
set diffDir $isdir
message "Launching 'GNU Diff'"
launch "$HOME:Tools:GNU Diff"
message "Starting diff…"
set dtext [dosc -n "GNU Diff" -s "$diffFlags \"[trimnum $diffOne]\" \"[trimnum $diffTwo]\""]
message "Starting diff…done"
if {![string length $dtext]} {
alertnote "No difference:\r$diffOne\r$diffTwo"
return
}
set top [expr $tileTop + $tileHeight - 178]
new -n $name -g $tileLeft $top [expr $tileWidth - 6] 178
changeMode [set winModes([lindex [winNames] 0]) Diff]
insertText "\r$dtext\r"
goto 0
setWinInfo dirty 0
setWinInfo read-only 1
select 0 [nextLineStart 0]
}
proc upDiff {} {
set limit 0
set res [search -s -f 0 -r 1 {^[^- \r]+\r} [expr [getPos] - 1]]
set pos [lindex $res 0]
select $pos [nextLineStart $pos]
}
# set res [search -s -f 0 -r 1 {^[^-<>]} [expr [getPos] - 1]]
proc downDiff {} {
set limit 0
set res [search -s -f 1 -r 1 {^[^- \r]+\r} [expr [getPos] + 1]]
set pos [lindex $res 0]
display $pos
select $pos [nextLineStart $pos]
}
# set res [search -s -f 1 -r 1 {^[^-<>]} [expr [getPos] + 1]]
# n1 a n3,n4
# n1,n2 d n3
# n1,n2 c n3,n4
proc selectDiff {} {
global diffOne diffTwo diffDir
set text [getText [lineStart [getPos]] [expr [nextLineStart [getPos]] - 1]]
if {![regexp {[acd]} $text char]} return
set res [split $text $char]
if {![scan [lindex $res 0] "%d" one]} return
if {![scan [lindex $res 1] "%d" two]} return
if {$one == 1} {incr one}
if {$two == 1} {incr two}
if {$diffDir} {
set res [search -s -f 0 -r 1 "^diff.*\r" [getPos]]
set text [eval getText $res]
set len [llength $text]
set diffOne [lindex $text [expr $len - 2]]
set diffTwo [lindex $text [expr $len - 1]]
}
diffDisplay $diffOne [expr $one - 1] 1
diffDisplay $diffTwo [expr $two - 1] 0
if {$diffDir} {
if {![string match {* Directory Comparison *} [car [winNames -f]]]} {
catch {bringToFront "* Directory Comparison *"}
}
}
}
proc diffDisplay {wname row left} {
global diffDir
if {$diffDir} {
set geo [diffGeo $left]
if {[set res [lsearch [winNames -f] "$wname*"]] < 0} {
eval edit -g $geo [list $wname]
set wname [car [winNames -f]]
} else {
set wname [lindex [winNames -f] $res]
if {[getGeometry $wname] != $geo} {
sizeWin $wname [lindex $geo 2] [lindex $geo 3]
moveWin $wname [lindex $geo 0] [lindex $geo 1]
}
if {$res > 2} {
bringToFront $wname
}
}
}
display -w $wname [rowColToPos -w $wname $row 0]
}
proc diffSel {wname ro row rowd left} {
global diffDir
if {$diffDir} {
set geo [diffGeo $left]
if {[set res [lsearch [winNames -f] "$wname*"]] < 0} {
eval edit -g $geo [list $wname]
set wname [car [winNames -f]]
} else {
set wname [lindex [winNames -f] $res]
if {[getGeometry $wname] != $geo} {
sizeWin $wname [lindex $geo 2] [lindex $geo 3]
moveWin $wname [lindex $geo 0] [lindex $geo 1]
}
}
}
bringToFront $wname
display [rowColToPos $ro 0]
select [rowColToPos $row 0] [rowColToPos [expr $rowd + 1] 0]
}
# goto [rowColToPos $row 0]
proc viewDiff {} {
global diffOne diffTwo diffDir
set text [getText [lineStart [getPos]] [expr [nextLineStart [getPos]] - 1]]
if {![regexp {[acd]} $text char]} return
set res [split $text $char]
if {![scan [lindex $res 0] "%d,%d" one oned]} return
if {![scan [lindex $res 1] "%d,%d" two twod]} return
set on $one
set tw $two
if {$on == 1} {incr on}
if {$tw == 1} {incr tw}
if {![info exists oned]} {set oned $one}
if {![info exists twod]} {set twod $two}
if {$diffDir} {
set res [search -s -f 0 -r 1 "^diff.*\r" [getPos]]
set text [eval getText $res]
set diffOne [lindex $text 1]
set diffTwo [lindex $text 2]
}
diffSel $diffOne [expr $on - 1] $one $oned 1
diffSel $diffTwo [expr $tw - 1] $two $twod 0
if {$diffDir} {
catch {bringToFront "* Directory Comparison *"}
} else {
catch {bringToFront "* File Comparison *"}
}
}
proc diffWin {} {
global winModes
set files [winNames -f]
set len [llength $files]
for {set i 0} {$i < $len} {incr i} {
if {$winModes([lindex [winNames -f] $i]) == "Diff"} {
bringToFront [lindex [winNames] $i]
return
}
}
beep
message "No Diff window."
}
bind '\r' selectDiff Diff
bind '\t' viewDiff Diff
bind Kpad. <c> diffWin
bind Enter {downDiff;selectDiff} Diff
bind Kpad0 {upDiff;selectDiff} Diff
bind down downDiff Diff
bind up upDiff Diff